#include "compile_run.hpp"
#include "../comm/httplib.h"
// #include <string>

using namespace ns_compile_and_run;
using namespace httplib;

void Usage(std::string proc)
{
    std::cerr << "Usage: " << "\n\t" << proc << " port" << std::endl;
}

//./compile_server port
int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        Usage(argv[0]);
        return 1;
    }

    uint32_t port = atoi(argv[1]);

    Server svr;
    svr.Post("/compile_and_run", [](const Request &req, Response &resp)
             {
                 // 用户发来的请求正文就是一个 json 串
                 std::string in_json = req.body;
                 std::string out_json;
                 if (!in_json.empty())
                 {
                     CompileAndRun::Start(in_json, &out_json);
                     resp.set_content(out_json, "application/json;charset=utf-8");
                 }
             });

    svr.listen("0.0.0.0", port);
}